/**
* All Rights Reserved , Copyright (C) 2022 , 青岛鼎信通讯股份有限公司
*
* MpmProjectFishFirstService
* 项目任务鱼骨图一级
*
* 修改纪录
* 2022-11-09 版本：1.0 houyuhang 创建。
* @version 版本：1.0
* @author 作者：houyuhang
* 创建日期：2022-11-09
*/
package com.topscomm.mpm.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.topscomm.basic.BasicEntity;
import com.topscomm.mpm.pojo.MpmProjectFishFirstEntity;
import com.topscomm.mpm.pojo.MpmProjectFishSecondEntity;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.topscomm.mpm.service.IMpmProjectFishFirstService;
import com.topscomm.mpm.service.impl.auto.MpmProjectFishFirstServiceAuto;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service(value = "mpmProjectFishFirstService")
@Scope(value = "singleton")
public class MpmProjectFishFirstService extends MpmProjectFishFirstServiceAuto implements IMpmProjectFishFirstService {
    @Autowired
    private MpmProjectFishSecondService mpmProjectFishSecondService;
    /**
     * @author: houyuhang
     * @date: 2022-11-09
     * @param paraMap
     * @return
     * @description:获取任务对应的鱼骨图
     * @modify:
     */
    @Transactional
    public List<Map<String, Object>> queryFishInfo(Map<String, Object> paraMap) {
        // 获取鱼骨图一级内容(客户部门)
        String taskid = ConvertUtil.convertToString(paraMap.get("taskid"));
        StringBufferProxy sql = new StringBufferProxy();
        sql.appendSingle(" enabled = 1 and taskid = '{0}' ", taskid);
        List<Map<String, Object>> mpmProjectFishFirstEntities = this.queryMapByWhere(sql.toString());
        if (CollectionUtil.isEmpty(mpmProjectFishFirstEntities)) {
            return new ArrayList<>();
        }
        // 获取鱼骨图二级内容(岗位)
        List<Map<String, Object>> mpmProjectFishSecondEntities = mpmProjectFishSecondService.queryMapByWhere(sql.toString());
        Map<String, List<Map<String, Object>>> mpmProjectFishSecondMap = mpmProjectFishSecondEntities.stream().collect(Collectors.groupingBy(item -> ConvertUtil.convertToString(item.get("fid"))));
        // 拼接鱼骨图二级内容
        for (Map<String, Object> mpmProjectFishFirstEntity : mpmProjectFishFirstEntities) {
            String firstId = ConvertUtil.convertToString(mpmProjectFishFirstEntity.get("id"));
            List<Map<String, Object>> tempSecondEntity = CollectionUtil.isEmpty(mpmProjectFishSecondMap.get(firstId)) ? new ArrayList<>() : mpmProjectFishSecondMap.get(firstId);
            mpmProjectFishFirstEntity.put("children", tempSecondEntity);
        }
        return mpmProjectFishFirstEntities;
    }
    /**
     * @author: houyuhang
     * @date: 2022-11-09
     * @param paraMap
     * @return
     * @description:任务鱼骨图数据全删全增
     * @modify:
     */
    @Transactional
    public void insertFishInfo(Map<String, Object> paraMap) {
        long taskid = ConvertUtil.convertToLong(paraMap.get("taskid"));
        StringBufferProxy sql = new StringBufferProxy();
        sql.appendSingle(" taskid = '{0}' ", taskid);
        // 删除一级、二级鱼骨图
        this.deleteByWhere(sql.toString());
        mpmProjectFishSecondService.deleteByWhere(sql.toString());
        // 新增一级、二级鱼骨图
        List<Map<String, Object>> fishboneList = (List<Map<String, Object>>) paraMap.get("fishbone");
        for (Map<String, Object> fishFirstMap : fishboneList) {
            // 新增鱼骨图一级
            MpmProjectFishFirstEntity mpmProjectFishFirstEntity = new MpmProjectFishFirstEntity();
            mpmProjectFishFirstEntity.convertFromMap(fishFirstMap);
            mpmProjectFishFirstEntity.setTaskid(taskid);
            mpmProjectFishFirstEntity.setId(0L);
            mpmProjectFishFirstEntity = this.insert(mpmProjectFishFirstEntity);
            // 新增鱼骨图二级
            List<BasicEntity> basicEntities = new ArrayList<>();
            for (Map<String, Object> fishSecondMap : (List<Map<String, Object>>) fishFirstMap.get("children")) {
                fishSecondMap.remove("id");
                MpmProjectFishSecondEntity mpmProjectFishSecondEntity = new MpmProjectFishSecondEntity();
                mpmProjectFishSecondEntity.convertFromMap(fishSecondMap);
                mpmProjectFishSecondEntity.setTaskid(taskid);
                mpmProjectFishSecondEntity.setFid(mpmProjectFishFirstEntity.getId());
                mpmProjectFishSecondEntity.setId(0L);
                basicEntities.add(mpmProjectFishSecondEntity);
            }
            mpmProjectFishSecondService.insertBatch(basicEntities);
        }
    }
}
